<?php
App::uses('AdminbaseController', 'Controller');
class AdminController extends AdminbaseController {

    public $layout = 'admin_main'; 
    public $components = array('Paginator');   

    function beforeFilter() {
        parent::beforeFilter();
    }
	
	//后台首页

    function index(){   

    }

    //TDK设置

    function tdk_add(){  
        $params = $this->request->data;                             //post的方法获取所有的值
        $id = $this->request->query('id');                           //get的方法获取id值
        $cid = $this->request->query('cid');                         //get的方法获取cid值
        if($this->request->is('post')){                         //如果成功获取请求参数
            if(empty($params['web_title'])){
                $this->ajaxReturn(4001, '请填写网站标题');    //判断数据是否输入    
            }
            if(empty($params['web_keyword'])){
                $this->ajaxReturn(4002, '请填写网站关键词');             
            }
            if(empty($params['web_descrion'])){
                $this->ajaxReturn(4003, '请填写网站描述');              
            } 
            if(!empty($id)){
                $this->loadModel('Tdk');  
                $tdk=array();
                $tdk['Tdk']['id'] = $id; 
                $tdk['Tdk']['cid'] = $cid;                 //写入数值
                $tdk['Tdk']['web_title'] = $params['web_title'];
                $tdk['Tdk']['web_keyword'] = $params['web_keyword'];
                $tdk['Tdk']['web_descrion'] = $params['web_descrion'];
                if(!$this->Tdk->save($tdk)){                          //存储数据，如果失败则报错
                    $this->ajaxReturn(4004, '操作失败，请稍后再试'); 
                }else{
                    $this->ajaxReturn(200, '操作成功！','','/admin/tdk_manage');
                }
            }else{
                $this->loadModel('Tdk');                             //加载数据表tdk
                $tdks = $this ->Tdk->find('first',array(            //通过id寻找数据表里是否有相同的id
                    'conditions' => array(
                        'cid' => $params['cid']
                        )
                    )
                );
                if(!empty($tdks)){                              //如果id为空，或者$tdk_name有的话
                    $this->ajaxReturn(4009, '已添加该栏目的TDK'); 
                }
                $tdk=array();
                $tdk['Tdk']['cid'] = $params['cid'];                 //写入数值
                $tdk['Tdk']['web_title'] = $params['web_title'];
                $tdk['Tdk']['web_keyword'] = $params['web_keyword'];
                $tdk['Tdk']['web_descrion'] = $params['web_descrion'];
                if(!$this->Tdk->save($tdk)){                          //存储数据，如果失败则报错
                    $this->ajaxReturn(4004, '操作失败，请稍后再试'); 
                }else{
                    $this->ajaxReturn(200, '操作成功！','','/admin/tdk_manage');
                }
            }
        }
        if(!empty($id)){                                //如果ID存在
            $this->loadModel('Column');                             //加载数据表column
            $this->loadModel('Tdk');                                //加载数据表tdk
            $coll = $this->Column->find("first",array('conditions'=>array('id'=>$cid)));   //通过cid在表里找id等于cid的数据
            if(!empty($coll)){
            $coll_name=$coll['Column']['col_name'];
            }else{
            $coll_name='网站默认';   
            }
            $this->set('coll_name',$coll_name);
        }else{
            $coll_name = '';
            $this->set('coll_name',$coll_name);
        }
        $this->loadModel('Tdk');                                //加载数据表tdk
        $tdk_id = $this ->Tdk->findById($id);
        $this->set('tdk_id',$tdk_id);
        $this->loadModel('Column');                             //加载数据表column
        $column = $this ->Column->find('all');
        $this->set('col',$column);                              //取出$column的数据输出到视图层，变量为$col
        $nav="a2";
        $this->set('nav',$nav);
        $mav="aa";
        $this->set('mav',$mav);
    }

    //TDK管理

    function tdk_manage(){
        $id = $this->request->query('id');
        $action = $this->request->query('action');
        $this->loadModel('Tdk');                                  //加载数据表tdk
        $this->loadModel('Column');                               //加载数据表column
        $this->paginate = array(                                        //利用分页取出数据
            'order'=>array('id' =>'desc'),
            'limit'=>10
        );
        try{
            $tdk = $this->paginate('Tdk');
        }catch(NotFoundException $e){
            $tdk = array();
        }
        foreach ($tdk as $key => $value) { 
        if(intval($value['Tdk']['cid'])>0){ 
            $info = $this->Column->findById(intval($value['Tdk']['cid']));
            if(isset($info['Column']['id'])){   
                $tdk[$key]['Tdk']['col_name']=$info['Column']['col_name'];
                }else{
                    $tdk[$key]['Tdk']['col_name'] = "栏目未定义";
                }
            }else{
                $tdk[$key]['Tdk']['col_name'] ="网站默认";
            }
        } 
        if(!empty($action) && !empty($id)){
            if(!$this->Tdk->delete(array('id'=>$id))){                        //存储数据，如果失败则报错
            $this->ajaxReturn(4004, '操作失败，请稍后再试'); 
            }else{
                $this->ajaxReturn(300, '操作成功！');
            }

        }
        $column = $this ->Column ->find('all',array());
        $this->set('column',$column); 
        $this->set('tdk',$tdk);                                 //把数据显示在视图页面上
        $nav="a2";
        $this->set('nav',$nav);
        $mav="aa";
        $this->set('mav',$mav);      
    }

    //TDK修改
    function tdk_update(){
        $this->tdk_add();                                              //加载nav_add的方法
        $this->render('/admin/tdk_add');                               //跳回添加页面
        $nav="a2";
        $this->set('nav',$nav);
        $mav="aa";
        $this->set('mav',$mav);
    }

    //TDK删除
    function tdk_del(){                                         
        $params = $this->request->query;                                //获取数据
        $this->data_delete('Tdk',array('id'=>$params['id']));        //调用父类方法data_delete() 
        $this->render('/admin/tdk_add');                             //跳回管理页面
    }

    //栏目添加
    function nav_add(){
    	$params = $this->request->data;                             //获取post数据
        $id = $this->request->query('id');
        $this->loadModel('Column');                             //加载数据表column
            if($this->request->is('post')){                         //如果成功获取请求参数
                if(empty($params['col_name'])){
                    $this->ajaxReturn(4001, '请填写栏目名称');      //判断数据是否输入    
                }
                $column = $this ->Column->find('first',array(            //通过id寻找数据表里是否有相同的id
                	'conditions' => array(
                		'col_name' => $params['col_name']
                		)
                	)
                );
                if(empty($id) && !empty($column)){                              //如果id为空，或者$column有的话
                	$this->ajaxReturn(4001, '添加的栏目已存在'); 
                }
                if(!empty($id)){
                    $column=array();
                	$column['Column']['id'] = $id;                               //写入数值
                	$column['Column']['col_name'] = $params['col_name'];
                	if(!$this->Column->save($column)){                          //存储数据，如果失败则报错
                        $this->ajaxReturn(4004, '操作失败，请稍后再试'); 
                    }else{
                        $this->ajaxReturn(200, '操作成功！','','/admin/nav_manage');
                    }
                }else{
                    $column=array();
    				$column['Column']['col_name'] = $params['col_name'];                 //写入数值
    		        if(!$this->Column->save($column)){                          //存储数据，如果失败则报错
    		        	$this->ajaxReturn(4004, '操作失败，请稍后再试'); 
    		        }else{
                        $this->ajaxReturn(200, '操作成功！','','/admin/nav_manage');
                    }
    		    }
            } 
        if(!empty($id)){                                         //如果ID存在
            $column = $this->Column->findById($id);                  //通过ID找到数据
            $this->set('column',$column);                            //加载数据显示到视图层
        }
        $nav="a1";
        $this->set('nav',$nav);
        $mav="aa";
        $this->set('mav',$mav);
    }

    //栏目修改
    function nav_update(){
    	$this->nav_add();                                              //加载nav_add的方法
    	$this->render('/admin/nav_add');                               //跳回添加页面
        $nav="a1";
        $mav="aa";
        $this->set('mav',$mav);
        $this->set('nav',$nav);
    }

    //栏目删除
    function nav_del(){                                         
    	$params = $this->request->query;                                //获取数据
    	$this->data_delete('Column',array('id'=>$params['id']));        //调用父类方法data_delete()
        $this->render('/admin/nav_manage');                             //跳回管理页面
    }

    //栏目管理

    function nav_manage(){
		$this->loadModel('Column');                                  //加载数据表column
        $id = $this->request->query('id');
        $action = $this->request->query('action');
        if(!empty($action) && !empty($id)){
            if(!$this->Column->delete(array('id'=>$id))){                        //存储数据，如果失败则报错
                $this->ajaxReturn(4004, '操作失败，请稍后再试'); 
            }else{
                $this->ajaxReturn(300, '操作成功！');
                $this->render('/admin/nav_manage');                               //跳回添加页面 
            } 
        }
        $this->paginate = array(                                        //利用分页取出数据
            'order'=>array('id' =>'desc'),
            'limit'=>10
        );
        try{
            $column = $this->paginate('Column');
        }catch(NotFoundException $e){
            $column = array();
        }
		$this->set('column',$column);                                 //把数据显示在视图页面上
        $nav="a1";
        $this->set('nav',$nav);
        $mav="aa";
        $this->set('mav',$mav);
    }

    //新增友情链接

    function friendlink_add(){ 
        $params = $this->request->data;                             //获取post数据  
        $this->loadModel('Friendlink');                                  //加载数据表guestbook
        $id = $this->request->query('id');
        if($this->request->is('post')){                         //如果成功获取请求参数
            if(empty($params['name'])){
                $this->ajaxReturn(4001, '请填写友情链接名称');      //判断数据是否输入    
            }
            $fr = array();
            $fr['Friendlink']['name'] = $params['name'];
            $fr['Friendlink']['link'] = $params['link'];
            if(!empty($id)){
                $fr['Friendlink']['id'] = $id;
                if(!$this->Friendlink->save($fr)){                          //存储数据，如果失败则报错
                    $this->ajaxReturn(4004, '操作失败，请稍后再试'); 
                }else{
                    $this->ajaxReturn(200, '操作成功！','','/admin/friendlink_manage');
                }
            }else{
                if(!$this->Friendlink->save($fr)){                          //存储数据，如果失败则报错
                    $this->ajaxReturn(4004, '操作失败，请稍后再试'); 
                }else{
                    $this->ajaxReturn(200, '操作成功！','','/admin/friendlink_manage');
                }
            }
        }
        if(!empty($id)){
            $fr = $this->Friendlink->find('first',array('conditions'=>array('id' => $id)));
            $this->set('fr',$fr);
        }
        $nav="a3";
        $this->set('nav',$nav);
        $mav="aa";
        $this->set('mav',$mav);
    }

    //友情链接管理

    function friendlink_manage(){   
        $this->loadModel('Friendlink');                                  //加载数据表hurdemand
        $id = $this->request->query('id');
        $action = $this->request->query('action');
        $fr = $this->Friendlink->find('all',array());
        if(!empty($action) && !empty($id)){
            if(!$this->Friendlink->delete(array('id'=>$id))){                        //存储数据，如果失败则报错
                $this->ajaxReturn(4004, '操作失败，请稍后再试'); 
            }else{
                $this->ajaxReturn(300, '操作成功！');
                $this->render('/admin/friendlink_manage');                               //跳回添加页面 
            } 
        }
        $this->set('fr',$fr);
        $nav="a3";
        $this->set('nav',$nav);
        $mav="aa";
        $this->set('mav',$mav);
    }

    //新增图片

    function img_add(){
        $params = $this->request->data;           
        if($this->request->is('post')){                         //如果成功获取请求参数
            if(empty($params['img'])){
                $this->ajaxReturn(4001, '请添加需要上传的图片');      //判断数据是否输入    
            }else{
                $this->ajaxReturn(200, '操作成功！','','/admin/img_manage');
            }
        }
        $nav="a4";
        $this->set('nav',$nav);
        $mav="aa";
        $this->set('mav',$mav);
    }

    //图片管理

    function img_manage(){
        $params = $this->request->query;
        $dir = 'image';
        $_GET['dir'] = $dir;
        $_GET['order'] = 'NAME';
        if(!empty($params['path'])){
            $_GET['path'] = $params['path'].'/';
        }
        $file = $this->file_manager(false);
        $file_list = array();
        $i = 1;
        foreach ($file['file_list'] as $key => $val) {
            if(isset($file_list[$val['time']])){
                $file_list[$val['time'] + $i] = $val;
                $i++;
            }else{
                $file_list[$val['time']] = $val;
            }
        }
        krsort($file_list);
        $file['file_list'] = $file_list;
        $this->set('file',$file);
        $nav="a4";
        $this->set('nav',$nav);
        $mav="aa";
        $this->set('mav',$mav);
    }

    //新增账号

    function admin_user_add(){   
        $params = $this->request->data;                             //获取post数据 
        $id = $this->request->query('id');
        $this->loadModel('AdminUser');                             //加载数据表AdminUser
        $this->loadModel('Role');                             //加载数据表AdminUser  
        if($this->request->is('post')){
            if(empty($params['username'])){
                $this->ajaxReturn(4001, '请填写用户名');      //判断数据是否输入    
            }
            if(empty($params['password'])){
                $this->ajaxReturn(4001, '请填写密码');      //判断数据是否输入    
            }
            if(empty($params['cpassword'])){
               $this->ajaxReturn(4001, '请填写确认密码');      //判断数据是否输入    
            }
            if($params['password']!=$params['cpassword']){
                $this->ajaxReturn(4001, '请填写确认密码');      //判断密码和确认密码是否一样 
            }
            $user = $this->AdminUser->find('first',array(      //通过用户名寻找是否有匹配的
                    'conditions' => array(
                        'username' => $params['username'], 
                        )
                    )
                );
                if(empty($id) && !empty($user)){ //insert check
                    $this->ajaxReturn(4001, '该账号已经有了');
                }
                if(!empty($id)){ //update
                    $user['AdminUser']['id'] = $id;
                    $user['AdminUser']['username'] = $params['username'];
                    $user['AdminUser']['c_name'] = $params['c_name'];
                    $user['AdminUser']['role'] = $params['role'];
                    $user['AdminUser']['password'] = md5($params['password']);
                    $user['AdminUser']['create_time'] = date("Y-m-d",time());
                    if(!$this->AdminUser->save($user)){                          //存储数据，如果失败则报错
                        $this->ajaxReturn(4004, '操作失败，请稍后再试'); 
                    }else{
                        $this->ajaxReturn(200, '操作成功！','','/admin/admin_user_manage');
                    }
                }else{ //insert
                    $db_data = $params;
                    $db_data['password'] = md5($params['password']);
                    $db_data['create_time'] = date("Y-m-d",time());
                    if(!$this->AdminUser->save($db_data)){                          //存储数据，如果失败则报错
                        $this->ajaxReturn(4004, '操作失败，请稍后再试'); 
                    }else{
                        $this->ajaxReturn(200, '操作成功！','','/admin/admin_user_manage');
                    }
                }
            }
        $roles = $this->Role->find('all',array());
        if(!empty($id)){
            $yy = $this->AdminUser->findById($id);
            $info = $this->Role->findById(intval($yy['AdminUser']['role']));
            if(!empty($info)){
               $yy['AdminUser']['role_name']=$info['Role']['role'];
            }else{
                $yy['AdminUser']['role_name']="角色已删";
            }
            $this->set('yy',$yy);
        }
        $this->set('roles',$roles);
        $nav="b1";
        $this->set('nav',$nav);
        $mav2="bb";
        $this->set('mav2',$mav2);
    }

    //账号修改
    function admin_user_update(){
        $this->admin_user_add();                                              
        $this->render('/admin/admin_user_add'); 
        $nav="b1";
        $this->set('nav',$nav);  
        $mav2="bb";
        $this->set('mav2',$mav2);                            
    }

    //账号删除
    function admin_user_del(){                                         
        $params = $this->request->query;                                //获取数据
        $this->data_delete('AdminUser',array('id'=>$params['id']));        //调用父类方法data_delete()
        $this->render('/admin/admin_user_manage');                             //跳回管理页面
    }

    //账号管理

    function admin_user_manage(){ 
        $this->loadModel('AdminUser');                             //加载数据表AdminUser
        $adminuser = $this->AdminUser->find('all',array());
        $id = $this->request->query('id');
        $action = $this->request->query('action');
        if(!empty($action) && !empty($id)){
            if(!$this->AdminUser->delete(array('id'=>$id))){                        //存储数据，如果失败则报错
                $this->ajaxReturn(4004, '操作失败，请稍后再试'); 
            }else{
                $this->ajaxReturn(300, '操作成功！');
                $this->render('/admin/admin_user_manage');                               //跳回添加页面 
            } 
        }
        $this->set('adminuser',$adminuser);
        $nav="b1";
        $this->set('nav',$nav);
        $mav2="bb";
        $this->set('mav2',$mav2);
    }

    //添加角色

    function role_add(){ 
        $params = $this->request->data;                             //获取post数据   
        $this->loadModel('Role');                             //加载数据表
        $id = $this->request->query('id');
        if($this->request->is('post')){
            if(empty($params['role'])){
                $this->ajaxReturn(4001, '请填写角色名称');      //判断数据是否输入    
            }
            $role = $this->Role->find('first',array(      //通过用户名寻找是否有匹配的
                    'conditions' => array(
                        'role' => $params['role'], 
                        )
                    )
            );
            if(!empty($role)){ //insert check
                $this->ajaxReturn(4001, '该角色已经有了');
            }
            if(!empty($id)){ //update
                $role['Role']['id'] = $id;
                $role['Role']['role'] = $params['role'];
                if(!$this->Role->save($role)){                          //存储数据，如果失败则报错
                    $this->ajaxReturn(4004, '操作失败，请稍后再试'); 
                }else{
                    $this->ajaxReturn(200, '操作成功！','','/admin/role_manage');
                }
            }else{ //insert
                $role['Role']['role'] = $params['role'];
                if(!$this->Role->save($role)){                          //存储数据，如果失败则报错
                    $this->ajaxReturn(4004, '操作失败，请稍后再试'); 
                }else{
                    $this->ajaxReturn(200, '操作成功！','','/admin/role_manage');
                }
            }
        }
        if(!empty($id)){
            $role = $this->Role->find('first',array( 
                'conditions' => array(
                        'id' => $id 
                        )
                    )
            );
        $this->set('role',$role);
        } 
        $nav="b2";
        $this->set('nav',$nav);   
        $mav2="bb";
        $this->set('mav2',$mav2);
    }

    //角色修改
    function role_update(){
        $this->role_add();                                              
        $this->render('/admin/role_add'); 
        $nav="b2";
        $this->set('nav',$nav);  
        $mav2="bb";
        $this->set('mav2',$mav2);                            
    }

    //角色删除
    function role_del(){                                         
        $params = $this->request->query;                                //获取数据
        $this->data_delete('Role',array('id'=>$params['id']));        //调用父类方法data_delete()
        $this->render('/admin/role_manage');                             //跳回管理页面
    }

    //角色管理

    function role_manage(){   
        $this->loadModel('Role');                             //加载数据表AdminUser
        $id = $this->request->query('id');
        $action = $this->request->query('action');
        if(!empty($action) && !empty($id)){
            if(!$this->Role->delete(array('id'=>$id))){                        //存储数据，如果失败则报错
                $this->ajaxReturn(4004, '操作失败，请稍后再试'); 
            }else{
                $this->ajaxReturn(300, '操作成功！');
                $this->render('/admin/role_manage');                               //跳回添加页面 
            } 
        }
        $role = $this->Role->find('all',array());
        $this->set('role',$role);
        $nav="b2";
        $this->set('nav',$nav);
        $mav2="bb";
        $this->set('mav2',$mav2);
    }
    //权限管理

    function admin_permissions(){ 
        $params = $this->request->data;                                //获取数据  
        $this->loadModel('Role');                             //加载数据表AdminUser
        $id = $this->request->query('id');
        if($this->request->is('post')){
            $role['Role']['id'] = $id;
            $num=[];
            $num[]=isset($params['l'])?'l':'';                 //判断栏目是否勾选
            $num[]=isset($params['t'])?'t':'';                 //判断tdk是否勾选
            $num[]=isset($params['f'])?'f':'';                 //判断友情链接是否勾选
            $num[]=isset($params['i'])?'i':'';                 //判断图片管理是否勾选
            $num[]=isset($params['b'])?'b':'';                 //判断账号是否勾选
            $num[]=isset($params['a'])?'a':'';                 //判断角色是否勾选
            $num[]=isset($params['wp'])?'wp':'';               //判断页面专区是否勾选
            $num[]=isset($params['n'])?'n':'';                 //判断新闻专区是否勾选
            $num[]=isset($params['p'])?'p':'';                 //判断产品专区是否勾选
            $num[]=isset($params['c'])?'c':'';                 //判断案例专区是否勾选
            $num[]=isset($params['g'])?'g':'';                 //判断留言专区是否勾选
            $num[]=isset($params['j'])?'j':'';                 //判断招聘专区是否勾选
            $num[]=isset($params['u'])?'u':'';                 //判断会员专区是否勾选
            $role['Role']['num'] =implode(",", $num);                 //把数组转化成字符串
                if(!$this->Role->save($role)){                          //存储数据，如果失败则报错
                    $this->ajaxReturn(4004, '操作失败，请稍后再试'); 
                }else{
                    $this->ajaxReturn(200, '操作成功！','','/admin/role_manage');
                }
            }
        $info = $this->Role->findById($id);
        if($info['Role']['num']=="s" ){
            $info['Role']['num']='l,t,f,i,b,a,wp,n,p,c,g,j,u';
        }
        $info['Role']['num'] = explode(",", $info['Role']['num']);                 //把字符串转化为数组
        $this->set('info',$info);
        $nav="b2";
        $this->set('nav',$nav);
        $mav2="bb";
        $this->set('mav2',$mav2);
    }

    //新增页面分类

    function webpage_class_add(){   
        $params = $this->request->data;                             //获取post数据
        $id = $this->request->query('id');
        $cid = $this->request->query('cid');
        $this->loadModel('WebpageClass');                             //加载数据表column
            if($this->request->is('post')){                         //如果成功获取请求参数
                if(empty($params['class_name'])){
                    $this->ajaxReturn(4001, '请填写分类名称');      //判断数据是否输入    
                }
                $webpage_class = $this ->WebpageClass->find('first',array(            //通过id寻找数据表里是否有相同的id
                    'conditions' => array(
                        'class_name' => $params['class_name']
                        )
                    )
                );
                if(!empty($webpage_class)){                              //如果id为空，或者$New_class有的话
                    $this->ajaxReturn(4001, '分类名已存在'); 
                }
                if(!empty($cid) and empty($id)){
                    $webpage_class=array();
                    $webpage_class['WebpageClass']['cid'] = $cid;                               //写入数值
                    $webpage_class['WebpageClass']['class_name'] = $params['class_name'];
                    if(!$this->WebpageClass->save($webpage_class)){                          //存储数据，如果失败则报错
                        $this->ajaxReturn(4004, '操作失败，请稍后再试'); 
                    }else{
                        $this->ajaxReturn(200, '操作成功！','','/admin/webpage_class_manage');
                    }
                }
                if(!empty($id)){
                    $webpage_class=array();
                    $webpage_class['WebpageClass']['id'] = $id;                               //写入数值
                    $webpage_class['WebpageClass']['class_name'] = $params['class_name'];
                    if(!$this->WebpageClass->save($webpage_class)){                          //存储数据，如果失败则报错
                        $this->ajaxReturn(4004, '操作失败，请稍后再试'); 
                    }else{
                        $this->ajaxReturn(200, '操作成功！','','/admin/webpage_class_manage');
                    }
                }else{
                    $webpage_class=array();  
                    $webpage_class['WebpageClass']['class_name'] = $params['class_name'];                 //写入数值
                    if(!$this->WebpageClass->save($webpage_class)){                          //存储数据，如果失败则报错
                        $this->ajaxReturn(4004, '操作失败，请稍后再试'); 
                    }else{
                        $this->ajaxReturn(200, '操作成功！','','/admin/webpage_class_manage');
                    }
                }
            }
        if(!empty($cid)){
            $cff = $this ->WebpageClass->find('first',array(            //通过id寻找数据表里是否有相同的id
                    'conditions' => array(
                        'id' => $cid
                        )
                    )
                );
            if(empty($cff)){
                $cf='木有';
            }else{
                $cf=$cff['WebpageClass']['class_name'];
            }
            $this->set('cf',$cf);
        }else{
            $cf='';
            $this->set('cf',$cf);
        } 
        if(!empty($id)){                                         //如果ID存在
            $webpage_class = $this->WebpageClass->findById($id);                  //通过ID找到数据
            $this->set('webpage_class',$webpage_class);                            //加载数据显示到视图层
        }
        $this->set('cid',$cid);
        $nav="c1";
        $this->set('nav',$nav);
        $mav3="bb";
        $this->set('mav3',$mav3);
    }

    //页面分类修改
    function webpage_class_update(){
        $this->webpage_class_add();                                              //加载webpage_add的方法
        $this->render('/admin/webpage_class_add');                               //跳回添加页面
        $nav="c1";
        $this->set('nav',$nav);
        $mav3="bb";
        $this->set('mav3',$mav3);
        }

    //页面分类删除
    function webpage_class_del(){                                         
        $params = $this->request->query;                                //获取数据
        $this->data_delete('WebpageClass',array('id'=>$params['id']));        //调用父类方法data_delete()
        $this->render('/admin/webpage_class_manage');                             //跳回管理页面
    }

    //页面分类管理

    function webpage_class_manage(){   
        $this->loadModel('WebpageClass');                                    //加载数据表WebpageClass
        $id = $this->request->query('id');
        $action = $this->request->query('action');
        if(!empty($action) && !empty($id)){
            if(!$this->WebpageClass->delete(array('id'=>$id))){                        //存储数据，如果失败则报错
                $this->ajaxReturn(4004, '操作失败，请稍后再试'); 
            }else{
                $this->ajaxReturn(300, '操作成功！');
                $this->render('/admin/webpage_class_manage');                               //跳回添加页面 
            } 
        }
        $this->paginate = array(                                             //利用分页取出数据
            'conditions' =>array('cid' => 0 ), 
            'order'=>array('id' =>'desc'),
            'limit'=>10
        );
        try{
            $big_class = $this->paginate('WebpageClass');
        }catch(NotFoundException $e){
            $big_class = array();
        }
        $small_class = $this->WebpageClass->find('all',array('conditions' =>array('cid !=' => 0 )));
        $this->set('big_class',$big_class);
        $this->set('small_class',$small_class);
        $nav="c1";
        $this->set('nav',$nav);
        $mav3="bb";
        $this->set('mav3',$mav3);
    }

    //新增页面

    function webpage_add(){   
        $params = $this->request->data;                             //获取post数据
        $id = $this->request->query('id');  
        $this->loadModel('Webpage');                                  //加载数据表webpage
        $this->loadModel('Tdk');                                  //加载数据表webpage
        $this->loadModel('WebpageClass');                               //加载数据表webpageclass
        if($this->request->is('post')){                         //如果成功获取请求参数
            if(empty($params['title'])){
                $this->ajaxReturn(4001, '请填写标题');      //判断数据是否输入    
            }
            // if(empty($params['class'])){
            //     $this->ajaxReturn(4001, '请选择分类');      //判断数据是否输入    
            // }
            // if($params['class']==1000){
            //     $this->ajaxReturn(4001, '该分类已删');      //判断数据是否输入    
            // }
            if(empty($params['content'])){
                $this->ajaxReturn(4001, '请添加内容');      //判断数据是否输入    
            }
            // if(!is_numeric($params['read_count'])){
            //     $this->ajaxReturn(4001, '阅读数是数字');      
            // }
            if(empty($id)){
                $webpage=array();
                $webpage['Webpage']['title'] = $params['title'];
                // $webpage['Webpage']['class'] = $params['class'];
                $webpage['Webpage']['min_content'] = $params['min_content'];
                $webpage['Webpage']['content'] = $params['content'];
                $webpage['Webpage']['web_title'] = $params['web_title'];
                $webpage['Webpage']['web_keyword'] = $params['web_keyword'];
                $webpage['Webpage']['web_descrion'] = $params['web_descrion'];
                $webpage['Webpage']['page_img'] = $params['page_img'];
                $webpage['Webpage']['create_time'] = date("Y-m-d",time());
                $webpage['Webpage']['update_time'] = date("Y-m-d",time());
                if(!$this->Webpage->save($webpage)){                          //存储数据，如果失败则报错
                    $this->ajaxReturn(4004, '操作失败，请稍后再试'); 
                }else{
                    $this->ajaxReturn(200, '操作成功！','','/admin/webpage_manage');
                }
            }else{
                $webpage=array();
                $webpage['Webpage']['id'] = $id;                               //写入数值
                $webpage['Webpage']['title'] = $params['title'];
                // $webpage['Webpage']['class'] = $params['class'];
                $webpage['Webpage']['min_content'] = $params['min_content'];
                $webpage['Webpage']['content'] = $params['content'];
                $webpage['Webpage']['web_title'] = $params['web_title'];
                $webpage['Webpage']['web_keyword'] = $params['web_keyword'];
                $webpage['Webpage']['web_descrion'] = $params['web_descrion'];
                $webpage['Webpage']['page_img'] = $params['page_img'];
                $webpage['Webpage']['update_time'] = date("Y-m-d",time());
                if(!$this->Webpage->save($webpage)){                          //存储数据，如果失败则报错
                    $this->ajaxReturn(4004, '操作失败，请稍后再试'); 
                }else{
                    $this->ajaxReturn(200, '操作成功！','','/admin/webpage_manage');
                }
            }
        }
        $tdk = $this->Tdk->find('first',array('conditions' => array('cid'=>0)));
        $webpageclass = $this->WebpageClass->find('all',array());
        if(!empty($id)){
            $webpage=$this->Webpage->findById($id);
            $nclass=[];
            $nclass['id']=$webpage['Webpage']['class'];
            $tclass=$this->WebpageClass->find('first',array('conditions' => array('id'=>$nclass['id'])));
            if(!empty($tclass)){
                $nclass['name']=$tclass['WebpageClass']['class_name'];
            }else{
                $nclass['name']="";
                $nclass['id']=1000;
            }
            $tdk['Tdk']['web_title']=$webpage['Webpage']['web_title'];
            $tdk['Tdk']['web_keyword']=$webpage['Webpage']['web_keyword'];
            $tdk['Tdk']['web_descrion']=$webpage['Webpage']['web_descrion'];
            $this->set('webpage',$webpage);
        }else{
            $nclass=[];
            $nclass['id']=0;
            $nclass['name']="选择分类";
        }
        $this->set('nclass',$nclass);
        $this->set('tdk',$tdk);
        $this->set('class',$webpageclass);
        $nav="c2";
        $this->set('nav',$nav);
        $mav3="bb";
        $this->set('mav3',$mav3);
    }

    //页面修改
    function webpage_update(){
        $this->webpage_add();                                              //加载webpage_add的方法
        $this->render('/admin/webpage_add');                               //跳回添加页面
        $nav="c2";
        $this->set('nav',$nav);
        $mav3="bb";
        $this->set('mav3',$mav3);
    }

    //页面删除
    function webpage_del(){                                         
        $params = $this->request->query;                                //获取数据
        $this->data_delete('Webpage',array('id'=>$params['id']));        //调用父类方法data_delete()
        $this->render('/admin/webpage_manage');                             //跳回管理页面
    }

    //页面管理

    function webpage_manage(){   
        $id = $this->request->query('id');
        $action = $this->request->query('action');
        $this->loadModel('Webpage');                                  //加载数据表news
        $this->loadModel('WebpageClass');                               //加载数据表newsclass
        $this->paginate = array(                                        //利用分页取出数据
            'conditions' =>array('class !=' => 1000 ),
            'order'=>array('id' =>'desc'),
            'limit'=>10
        );
        try{
            $webpage = $this->paginate('Webpage');
        }catch(NotFoundException $e){
            $webpage = array();
        }
        foreach ($webpage as $key => $value) { 
        if(intval($value['Webpage']['class'])>0){ 
            $info = $this->WebpageClass->findById(intval($value['Webpage']['class']));
            if(isset($info['WebpageClass']['id'])){   
                $webpage[$key]['Webpage']['class_name']=$info['WebpageClass']['class_name'];
                }else{
                    $webpage[$key]['Webpage']['class_name'] = "分类已删";
                }
            }else{
                $webpage[$key]['Webpage']['class_name'] ="分类已删";
            }
        } 
        if(!empty($action) && !empty($id)){
            if(!$this->Webpage->delete(array('id'=>$id))){                        //存储数据，如果失败则报错
                $this->ajaxReturn(4004, '操作失败，请稍后再试'); 
            }else{
                $this->ajaxReturn(300, '操作成功！');
                $this->render('/admin/webpage_manage');                               //跳回添加页面 
            }
        }
        $this->set('webpage',$webpage);
        $nav="c2";
        $this->set('nav',$nav);  
        $mav3="bb";
        $this->set('mav3',$mav3);                           
    }

    //新增新闻分类

    function news_class_add(){   
        $params = $this->request->data;                             //获取post数据
        $id = $this->request->query('id');
        $cid = $this->request->query('cid');
        $this->loadModel('NewsClass');                             //加载数据表column
            if($this->request->is('post')){                         //如果成功获取请求参数
                if(empty($params['class_name'])){
                    $this->ajaxReturn(4001, '请填写分类名称');      //判断数据是否输入    
                }
                $news_class = $this ->NewsClass->find('first',array(            //通过id寻找数据表里是否有相同的id
                    'conditions' => array(
                        'class_name' => $params['class_name']
                        )
                    )
                );
                if(!empty($news_class)){                              //如果id为空，或者$New_class有的话
                    $this->ajaxReturn(4001, '分类名已存在'); 
                }
                if(!empty($cid) and empty($id)){
                    $news_class=array();
                    $news_class['NewsClass']['cid'] = $cid;                               //写入数值
                    $news_class['NewsClass']['class_name'] = $params['class_name'];
                    if(!$this->NewsClass->save($news_class)){                          //存储数据，如果失败则报错
                        $this->ajaxReturn(4004, '操作失败，请稍后再试'); 
                    }else{
                        $this->ajaxReturn(200, '操作成功！','','/admin/news_class_manage');
                    }
                }
                if(!empty($id)){
                    $news_class=array();
                    $news_class['NewsClass']['id'] = $id;                               //写入数值
                    $news_class['NewsClass']['class_name'] = $params['class_name'];
                    if(!$this->NewsClass->save($news_class)){                          //存储数据，如果失败则报错
                        $this->ajaxReturn(4004, '操作失败，请稍后再试'); 
                    }else{
                        $this->ajaxReturn(200, '操作成功！','','/admin/news_class_manage');
                    }
                }else{
                    $news_class=array();  
                    $news_class['NewsClass']['class_name'] = $params['class_name'];                 //写入数值
                    if(!$this->NewsClass->save($news_class)){                          //存储数据，如果失败则报错
                        $this->ajaxReturn(4004, '操作失败，请稍后再试'); 
                    }else{
                        $this->ajaxReturn(200, '操作成功！','','/admin/news_class_manage');
                    }
                }
            }
        if(!empty($cid)){
            $cff = $this ->NewsClass->find('first',array(            //通过id寻找数据表里是否有相同的id
                    'conditions' => array(
                        'id' => $cid
                        )
                    )
                );
            if(empty($cff)){
                $cf='木有';
            }else{
                $cf=$cff['NewsClass']['class_name'];
            }
            $this->set('cf',$cf);
        }else{
            $cf='';
            $this->set('cf',$cf);
        } 
        if(!empty($id)){                                         //如果ID存在
            $news_class = $this->NewsClass->findById($id);                  //通过ID找到数据
            $this->set('news_class',$news_class);                            //加载数据显示到视图层
        }
        $this->set('cid',$cid);
        $nav="d1";
        $this->set('nav',$nav);
        $mav4="bb";
        $this->set('mav4',$mav4);    
    }

    //新闻分类修改
    function news_class_update(){
        $this->news_class_add();                                              //加载news_add的方法
        $this->render('/admin/news_class_add');                               //跳回添加页面
        $nav="d1";
        $this->set('nav',$nav);
        $mav4="bb";
        $this->set('mav4',$mav4); 
    }

    //新闻分类删除
    function news_class_del(){                                         
        $params = $this->request->query;                                //获取数据
        $this->data_delete('NewsClass',array('id'=>$params['id']));        //调用父类方法data_delete()
        $this->render('/admin/news_class_manage');                             //跳回管理页面
    }

    //新闻分类管理

    function news_class_manage(){   
        $this->loadModel('NewsClass');                                    //加载数据表NewsClass
        $id = $this->request->query('id');
        $action = $this->request->query('action');
        if(!empty($action) && !empty($id)){
            if(!$this->NewsClass->delete(array('id'=>$id))){                        //存储数据，如果失败则报错
                $this->ajaxReturn(4004, '操作失败，请稍后再试'); 
            }else{
                $this->ajaxReturn(300, '操作成功！');
                $this->render('/admin/news_class_manage');                               //跳回添加页面 
            } 
        }
        $this->paginate = array(                                             //利用分页取出数据
            'conditions' =>array('cid' => 0 ), 
            'order'=>array('id' =>'desc'),
            'limit'=>10
        );
        try{
            $big_class = $this->paginate('NewsClass');
        }catch(NotFoundException $e){
            $big_class = array();
        }
        $small_class = $this->NewsClass->find('all',array('conditions' =>array('cid !=' => 0 )));
        $this->set('big_class',$big_class);
        $this->set('small_class',$small_class);
        $nav="d1";
        $this->set('nav',$nav);
        $mav4="bb";
        $this->set('mav4',$mav4); 
    }

    //新增新闻

    function news_add(){ 
        $params = $this->request->data;                             //获取post数据
        $id = $this->request->query('id');  
        $this->loadModel('News');                                  //加载数据表news
        $this->loadModel('Tdk');                                  //加载数据表news
        $this->loadModel('NewsClass');                               //加载数据表newsclass
        if($this->request->is('post')){                         //如果成功获取请求参数
            if(empty($params['title'])){
                $this->ajaxReturn(4001, '请填写标题');      //判断数据是否输入    
            }
            if(empty($params['class'])){
                $this->ajaxReturn(4001, '请选择分类');      //判断数据是否输入    
            }
            if($params['class']==1000){
                $this->ajaxReturn(4001, '该分类已删');      //判断数据是否输入    
            }
            if(empty($params['content'])){
                $this->ajaxReturn(4001, '请添加内容');      //判断数据是否输入    
            }
            if(!is_numeric($params['read_count'])){
                $this->ajaxReturn(4001, '阅读数是数字');      
            }
            if(empty($id)){
                $news=array();
                $news['News']['title'] = $params['title'];
                $news['News']['class'] = $params['class'];
                $news['News']['min_content'] = $params['min_content'];
                $news['News']['content'] = $params['content'];
                $news['News']['web_title'] = $params['web_title'];
                $news['News']['web_keyword'] = $params['web_keyword'];
                $news['News']['web_descrion'] = $params['web_descrion'];
                $news['News']['news_img'] = $params['news_img'];
                $news['News']['news_index'] = isset($params['news_index'])?1:2;
                $news['News']['create_time'] = date("Y-m-d",time());
                $news['News']['update_time'] = date("Y-m-d",time());
                $news['News']['author'] = $params['author'];
                $news['News']['read_count'] = $params['read_count'];
                if(!$this->News->save($news)){                          //存储数据，如果失败则报错
                    $this->ajaxReturn(4004, '操作失败，请稍后再试'); 
                }else{
                    $this->ajaxReturn(200, '操作成功！','','/admin/news_manage');
                }
            }else{
                $news=array();
                $news['News']['id'] = $id;                               //写入数值
                $news['News']['title'] = $params['title'];
                $news['News']['class'] = $params['class'];
                $news['News']['min_content'] = $params['min_content'];
                $news['News']['content'] = $params['content'];
                $news['News']['web_title'] = $params['web_title'];
                $news['News']['web_keyword'] = $params['web_keyword'];
                $news['News']['web_descrion'] = $params['web_descrion'];
                $news['News']['news_img'] = $params['news_img'];
                $news['News']['news_index'] = isset($params['news_index'])?1:2;
                $news['News']['update_time'] = date("Y-m-d",time());
                $news['News']['author'] = $params['author'];
                $news['News']['read_count'] = $params['read_count'];
                if(!$this->News->save($news)){                          //存储数据，如果失败则报错
                    $this->ajaxReturn(4004, '操作失败，请稍后再试'); 
                }else{
                    $this->ajaxReturn(200, '操作成功！','','/admin/news_manage');
                }
            }
        }
        $tdk = $this->Tdk->find('first',array('conditions' => array('cid'=>0)));
        $newsclass = $this->NewsClass->find('all',array());
        if(!empty($id)){
            $news=$this->News->findById($id);
            $read_count=$news['News']['read_count'];
            $nclass=[];
            $nclass['id']=$news['News']['class'];
            $tclass=$this->NewsClass->find('first',array('conditions' => array('id'=>$nclass['id'])));
            if(!empty($tclass)){
                $nclass['name']=$tclass['NewsClass']['class_name'];
            }else{
                $nclass['name']="";
                $nclass['id']=1000;
            }
            $tdk['Tdk']['web_title']=$news['News']['web_title'];
            $tdk['Tdk']['web_keyword']=$news['News']['web_keyword'];
            $tdk['Tdk']['web_descrion']=$news['News']['web_descrion'];
            $this->set('news',$news);
            if($news['News']['news_index']==1){
                $checkbox=1;
            }else{
                $checkbox=0;
            }
        }else{
            $read_count=100;
            $nclass=[];
            $nclass['id']=0;
            $nclass['name']="选择分类";
            $checkbox=0;
        }
        $this->set('nclass',$nclass);
        $this->set('checkbox',$checkbox);
        $this->set('read_count',$read_count);
        $this->set('tdk',$tdk);
        $this->set('class',$newsclass);
        $nav="d2";
        $this->set('nav',$nav);
        $mav4="bb";
        $this->set('mav4',$mav4); 
    }

    //新闻修改
    function news_update(){
        $this->news_add();                                              //加载news_add的方法
        $this->render('/admin/news_add');                               //跳回添加页面
        $nav="d2";
        $this->set('nav',$nav);
        $mav4="bb";
        $this->set('mav4',$mav4); 
    }

    //新闻删除
    function news_del(){                                         
        $params = $this->request->query;                                //获取数据
        $this->data_delete('News',array('id'=>$params['id']));        //调用父类方法data_delete()
        $this->render('/admin/news_manage');                             //跳回管理页面
    }

    //新闻管理

    function news_manage(){   
        $id = $this->request->query('id');
        $action = $this->request->query('action');
        $this->loadModel('News');                                  //加载数据表news
        $this->loadModel('NewsClass');                               //加载数据表newsclass
        $this->paginate = array(                                        //利用分页取出数据
            'conditions' =>array('class !=' => 1000 ),
            'order'=>array('id' =>'desc'),
            'limit'=>10
        );
        try{
            $news = $this->paginate('News');
        }catch(NotFoundException $e){
            $news = array();
        }
        foreach ($news as $key => $value) { 
        if(intval($value['News']['class'])>0){ 
            $info = $this->NewsClass->findById(intval($value['News']['class']));
            if(isset($info['NewsClass']['id'])){   
                $news[$key]['News']['class_name']=$info['NewsClass']['class_name'];
                }else{
                    $news[$key]['News']['class_name'] = "分类已删";
                }
            }else{
                $news[$key]['News']['class_name'] ="分类已删";
            }
        } 
        if(!empty($action) && !empty($id)){
            if(!$this->News->delete(array('id'=>$id))){                        //存储数据，如果失败则报错
            $this->ajaxReturn(4004, '操作失败，请稍后再试'); 
            }else{
                $this->ajaxReturn(300, '操作成功！');
                $this->render('/admin/news_manage');                               //跳回添加页面 
            }

        }
        $this->set('news',$news);                                 //把数据显示在视图页面上   
        $nav="d2";
        $this->set('nav',$nav);   
        $mav4="bb";
        $this->set('mav4',$mav4); 
    }

    //新增产品分类

    function product_class_add(){   
        $params = $this->request->data;                             //获取post数据
        $id = $this->request->query('id');
        $cid = $this->request->query('cid');
        $this->loadModel('ProductClass');                             //加载数据表column
            if($this->request->is('post')){                         //如果成功获取请求参数
                if(empty($params['class_name'])){
                    $this->ajaxReturn(4001, '请填写分类名称');      //判断数据是否输入    
                }
                $product_class = $this ->ProductClass->find('first',array(            //通过id寻找数据表里是否有相同的id
                    'conditions' => array(
                        'class_name' => $params['class_name']
                        )
                    )
                );
                if(!empty($product_class)){                              //如果id为空，或者$New_class有的话
                    $this->ajaxReturn(4001, '分类名已存在'); 
                }
                if(!empty($cid) and empty($id)){
                    $product_class=array();
                    $product_class['ProductClass']['cid'] = $cid;                               //写入数值
                    $product_class['ProductClass']['class_name'] = $params['class_name'];
                    if(!$this->ProductClass->save($product_class)){                          //存储数据，如果失败则报错
                        $this->ajaxReturn(4004, '操作失败，请稍后再试'); 
                    }else{
                        $this->ajaxReturn(200, '操作成功！','','/admin/product_class_manage');
                    }
                }
                if(!empty($id)){
                    $product_class=array();
                    $product_class['ProductClass']['id'] = $id;                               //写入数值
                    $product_class['ProductClass']['class_name'] = $params['class_name'];
                    if(!$this->ProductClass->save($product_class)){                          //存储数据，如果失败则报错
                        $this->ajaxReturn(4004, '操作失败，请稍后再试'); 
                    }else{
                        $this->ajaxReturn(200, '操作成功！','','/admin/product_class_manage');
                    }
                }else{
                    $product_class=array();  
                    $product_class['ProductClass']['class_name'] = $params['class_name'];                 //写入数值
                    if(!$this->ProductClass->save($product_class)){                          //存储数据，如果失败则报错
                        $this->ajaxReturn(4004, '操作失败，请稍后再试'); 
                    }else{
                        $this->ajaxReturn(200, '操作成功！','','/admin/product_class_manage');
                    }
                }
            }
        if(!empty($cid)){
            $cff = $this ->ProductClass->find('first',array(            //通过id寻找数据表里是否有相同的id
                    'conditions' => array(
                        'id' => $cid
                        )
                    )
                );
            if(empty($cff)){
                $cf='木有';
            }else{
                $cf=$cff['ProductClass']['class_name'];
            }
            $this->set('cf',$cf);
        }else{
            $cf='';
            $this->set('cf',$cf);
        } 
        if(!empty($id)){                                         //如果ID存在
            $product_class = $this->ProductClass->findById($id);                  //通过ID找到数据
            $this->set('product_class',$product_class);                            //加载数据显示到视图层
        }
        $this->set('cid',$cid);
        $nav="e1";
        $this->set('nav',$nav);
        $mav5="bb";
        $this->set('mav5',$mav5); 
    }

    //产品分类修改
    function product_class_update(){
        $this->product_class_add();                                              //加载product_add的方法
        $this->render('/admin/product_class_add');                               //跳回添加页面
        $nav="e1";
        $this->set('nav',$nav);
        $mav5="bb";
        $this->set('mav5',$mav5); 
    }

    //产品分类删除
    function product_class_del(){                                         
        $params = $this->request->query;                                //获取数据
        $this->data_delete('ProductClass',array('id'=>$params['id']));        //调用父类方法data_delete()
        $this->render('/admin/product_class_manage');                             //跳回管理页面
    }

    //产品分类管理

    function product_class_manage(){   
        $this->loadModel('ProductClass');                                    //加载数据表ProductClass
        $id = $this->request->query('id');
        $action = $this->request->query('action');
        if(!empty($action) && !empty($id)){
            if(!$this->ProductClass->delete(array('id'=>$id))){                        //存储数据，如果失败则报错
                $this->ajaxReturn(4004, '操作失败，请稍后再试'); 
            }else{
                $this->ajaxReturn(300, '操作成功！');
                $this->render('/admin/product_class_manage');                               //跳回添加页面 
            } 
        }
        $this->paginate = array(                                             //利用分页取出数据
            'conditions' =>array('cid' => 0 ), 
            'order'=>array('id' =>'desc'),
            'limit'=>10
        );
        try{
            $big_class = $this->paginate('ProductClass');
        }catch(NotFoundException $e){
            $big_class = array();
        }
        $small_class = $this->ProductClass->find('all',array('conditions' =>array('cid !=' => 0 )));
        $this->set('big_class',$big_class);
        $this->set('small_class',$small_class);
        $nav="e1";
        $this->set('nav',$nav);
        $mav5="bb";
        $this->set('mav5',$mav5); 
    }

    //新增产品

    function product_add(){   
        $params = $this->request->data;                             //获取post数据
        $id = $this->request->query('id');  
        $this->loadModel('Product');                                  //加载数据表news
        $this->loadModel('Tdk');                                  //加载数据表news
        $this->loadModel('ProductClass');                               //加载数据表newsclass
        if($this->request->is('post')){                         //如果成功获取请求参数
            if(empty($params['title'])){
                $this->ajaxReturn(4001, '请填写标题');      //判断数据是否输入    
            }
            if(empty($params['class'])){
                $this->ajaxReturn(4001, '请选择分类');      //判断数据是否输入    
            }
            if($params['class']==1000){
                $this->ajaxReturn(4001, '该分类已删');      //判断数据是否输入    
            }
            if(empty($params['content'])){
                $this->ajaxReturn(4001, '请添加内容');      //判断数据是否输入    
            }
            if(!is_numeric($params['read_count'])){
                $this->ajaxReturn(4001, '阅读数是数字');      
            }
            if(empty($id)){
                $product=array();
                $product['Product']['title'] = $params['title'];
                $product['Product']['class'] = $params['class'];
                $product['Product']['min_content'] = $params['min_content'];
                $product['Product']['content'] = $params['content'];
                $product['Product']['web_title'] = $params['web_title'];
                $product['Product']['web_keyword'] = $params['web_keyword'];
                $product['Product']['web_descrion'] = $params['web_descrion'];
                $product['Product']['product_img'] = $params['product_img'];
                $product['Product']['product_index'] = isset($params['product_index'])?1:2;
                $product['Product']['create_time'] = date("Y-m-d",time());
                $product['Product']['update_time'] = date("Y-m-d",time());
                $product['Product']['author'] = $params['author'];
                $product['Product']['read_count'] = $params['read_count'];
                if(!$this->Product->save($product)){                          //存储数据，如果失败则报错
                    $this->ajaxReturn(4004, '操作失败，请稍后再试'); 
                }else{
                    $this->ajaxReturn(200, '操作成功！','','/admin/product_manage');
                }
            }else{
                $product=array();
                $product['Product']['id'] = $id;                               //写入数值
                $product['Product']['title'] = $params['title'];
                $product['Product']['class'] = $params['class'];
                $product['Product']['min_content'] = $params['min_content'];
                $product['Product']['content'] = $params['content'];
                $product['Product']['web_title'] = $params['web_title'];
                $product['Product']['web_keyword'] = $params['web_keyword'];
                $product['Product']['web_descrion'] = $params['web_descrion'];
                $product['Product']['product_img'] = $params['product_img'];
                $product['Product']['product_index'] = isset($params['product_index'])?1:2;
                $product['Product']['update_time'] = date("Y-m-d",time());
                $product['Product']['author'] = $params['author'];
                $product['Product']['read_count'] = $params['read_count'];
                if(!$this->Product->save($product)){                          //存储数据，如果失败则报错
                    $this->ajaxReturn(4004, '操作失败，请稍后再试'); 
                }else{
                    $this->ajaxReturn(200, '操作成功！','','/admin/product_manage');
                }
            }
        }
        $tdk = $this->Tdk->find('first',array('conditions' => array('cid'=>0)));
        $productclass = $this->ProductClass->find('all',array());
        if(!empty($id)){
            $product=$this->Product->findById($id);
            $read_count=$product['Product']['read_count'];
            $nclass=[];
            $nclass['id']=$product['Product']['class'];
            $tclass=$this->ProductClass->find('first',array('conditions' => array('id'=>$nclass['id'])));
            if(!empty($tclass)){
                $nclass['name']=$tclass['ProductClass']['class_name'];
            }else{
                $nclass['name']="";
                $nclass['id']=1000;
            }
            $tdk['Tdk']['web_title']=$product['Product']['web_title'];
            $tdk['Tdk']['web_keyword']=$product['Product']['web_keyword'];
            $tdk['Tdk']['web_descrion']=$product['Product']['web_descrion'];
            $this->set('product',$product);
            if($product['Product']['product_index']==1){
                $checkbox=1;
            }else{
                $checkbox=0;
            }
        }else{
            $read_count=100;
            $nclass=[];
            $nclass['id']=0;
            $nclass['name']="选择分类";
            $checkbox=0;
        }
        $this->set('nclass',$nclass);
        $this->set('checkbox',$checkbox);
        $this->set('read_count',$read_count);
        $this->set('tdk',$tdk);
        $this->set('class',$productclass);
        $nav="e2";
        $this->set('nav',$nav);
        $mav5="bb";
        $this->set('mav5',$mav5); 
    }
    //产品修改
    function product_update(){
        $this->product_add();                                              //加载product_add的方法
        $this->render('/admin/product_add');                               //跳回添加页面
        $nav="e2";
        $this->set('nav',$nav);
        $mav5="bb";
        $this->set('mav5',$mav5); 
    }

    //产品删除
    function product_del(){                                         
        $params = $this->request->query;                                //获取数据
        $this->data_delete('Product',array('id'=>$params['id']));        //调用父类方法data_delete()
        $this->render('/admin/product_manage');                             //跳回管理页面
    }

    //产品管理

    function product_manage(){   
        $id = $this->request->query('id');
        $action = $this->request->query('action');
        $this->loadModel('Product');                                  //加载数据表news
        $this->loadModel('ProductClass');                               //加载数据表newsclass
        $this->paginate = array(                                        //利用分页取出数据
            'conditions' =>array('class !=' => 1000 ),
            'order'=>array('id' =>'desc'),
            'limit'=>10
        );
        try{
            $product = $this->paginate('Product');
        }catch(NotFoundException $e){
            $product = array();
        }
        foreach ($product as $key => $value) { 
        if(intval($value['Product']['class'])>0){ 
            $info = $this->ProductClass->findById(intval($value['Product']['class']));
            if(isset($info['ProductClass']['id'])){   
                $product[$key]['Product']['class_name']=$info['ProductClass']['class_name'];
                }else{
                    $product[$key]['Product']['class_name'] = "分类已删";
                }
            }else{
                $product[$key]['Product']['class_name'] ="分类已删";
            }
        } 
        if(!empty($action) && !empty($id)){
            if(!$this->Product->delete(array('id'=>$id))){                        //存储数据，如果失败则报错
            $this->ajaxReturn(4004, '操作失败，请稍后再试'); 
            }else{
                $this->ajaxReturn(300, '操作成功！');
                $this->render('/admin/product_manage');                               //跳回添加页面 
            }
        }
        $this->set('product',$product); 
        $nav="e2";
        $this->set('nav',$nav);    
        $mav5="bb";
        $this->set('mav5',$mav5);                         
    }

    //新增案例分类

    function case_class_add(){   
        $params = $this->request->data;                             //获取post数据
        $id = $this->request->query('id');
        $cid = $this->request->query('cid');
        $this->loadModel('CaseClass');                             //加载数据表column
            if($this->request->is('post')){                         //如果成功获取请求参数
                if(empty($params['class_name'])){
                    $this->ajaxReturn(4001, '请填写分类名称');      //判断数据是否输入    
                }
                $case_class = $this ->CaseClass->find('first',array(            //通过id寻找数据表里是否有相同的id
                    'conditions' => array(
                        'class_name' => $params['class_name']
                        )
                    )
                );
                if(!empty($case_class)){                              //如果id为空，或者$New_class有的话
                    $this->ajaxReturn(4001, '分类名已存在'); 
                }
                if(!empty($cid) and empty($id)){
                    $case_class=array();
                    $case_class['CaseClass']['cid'] = $cid;                               //写入数值
                    $case_class['CaseClass']['class_name'] = $params['class_name'];
                    if(!$this->CaseClass->save($case_class)){                          //存储数据，如果失败则报错
                        $this->ajaxReturn(4004, '操作失败，请稍后再试'); 
                    }else{
                        $this->ajaxReturn(200, '操作成功！','','/admin/case_class_manage');
                    }
                }
                if(!empty($id)){
                    $case_class=array();
                    $case_class['CaseClass']['id'] = $id;                               //写入数值
                    $case_class['CaseClass']['class_name'] = $params['class_name'];
                    if(!$this->CaseClass->save($case_class)){                          //存储数据，如果失败则报错
                        $this->ajaxReturn(4004, '操作失败，请稍后再试'); 
                    }else{
                        $this->ajaxReturn(200, '操作成功！','','/admin/case_class_manage');
                    }
                }else{
                    $case_class=array();  
                    $case_class['CaseClass']['class_name'] = $params['class_name'];                 //写入数值
                    if(!$this->CaseClass->save($case_class)){                          //存储数据，如果失败则报错
                        $this->ajaxReturn(4004, '操作失败，请稍后再试'); 
                    }else{
                        $this->ajaxReturn(200, '操作成功！','','/admin/case_class_manage');
                    }
                }
            }
        if(!empty($cid)){
            $cff = $this ->CaseClass->find('first',array(            //通过id寻找数据表里是否有相同的id
                    'conditions' => array(
                        'id' => $cid
                        )
                    )
                );
            if(empty($cff)){
                $cf='木有';
            }else{
                $cf=$cff['CaseClass']['class_name'];
            }
            $this->set('cf',$cf);
        }else{
            $cf='';
            $this->set('cf',$cf);
        } 
        if(!empty($id)){                                         //如果ID存在
            $case_class = $this->CaseClass->findById($id);                  //通过ID找到数据
            $this->set('case_class',$case_class);                            //加载数据显示到视图层
        }
        $this->set('cid',$cid);
        $nav="f1";
        $this->set('nav',$nav);
        $mav6="bb";
        $this->set('mav6',$mav6); 
    }

    //案例分类修改
    function case_class_update(){
        $this->case_class_add();                                              //加载case_add的方法
        $this->render('/admin/case_class_add');                               //跳回添加页面
        $nav="f1";
        $this->set('nav',$nav);
        $mav6="bb";
        $this->set('mav6',$mav6); 
    }

    //案例分类删除
    function case_class_del(){                                         
        $params = $this->request->query;                                //获取数据
        $this->data_delete('CaseClass',array('id'=>$params['id']));        //调用父类方法data_delete()
        $this->render('/admin/case_class_manage');                             //跳回管理页面
    }

    //案例分类管理

    function case_class_manage(){   
        $this->loadModel('CaseClass');                                    //加载数据表CaseClass
        $id = $this->request->query('id');
        $action = $this->request->query('action');
        if(!empty($action) && !empty($id)){
            if(!$this->CaseClass->delete(array('id'=>$id))){                        //存储数据，如果失败则报错
                $this->ajaxReturn(4004, '操作失败，请稍后再试'); 
            }else{
                $this->ajaxReturn(300, '操作成功！');
                $this->render('/admin/case_class_manage');                               //跳回添加页面 
            } 
        }
        $this->paginate = array(                                             //利用分页取出数据
            'conditions' =>array('cid' => 0 ), 
            'order'=>array('id' =>'desc'),
            'limit'=>10
        );
        try{
            $big_class = $this->paginate('CaseClass');
        }catch(NotFoundException $e){
            $big_class = array();
        }
        $small_class = $this->CaseClass->find('all',array('conditions' =>array('cid !=' => 0 )));
        $this->set('big_class',$big_class);
        $this->set('small_class',$small_class);
        $nav="f1";
        $this->set('nav',$nav);
        $mav6="bb";
        $this->set('mav6',$mav6); 
    }

    //新增案例

    function case_add(){   
        $params = $this->request->data;                             //获取post数据
        $id = $this->request->query('id');  
        $this->loadModel('Case');                                  //加载数据表Case
        $this->loadModel('Tdk');                                  //加载数据表Tdk
        $this->loadModel('CaseClass');                               //加载数据表CaseClass
        if($this->request->is('post')){                         //如果成功获取请求参数
            if(empty($params['title'])){
                $this->ajaxReturn(4001, '请填写标题');      //判断数据是否输入    
            }
            if(empty($params['class'])){
                $this->ajaxReturn(4001, '请选择分类');      //判断数据是否输入    
            }
            if($params['class']==1000){
                $this->ajaxReturn(4001, '该分类已删');      //判断数据是否输入    
            }
            if(empty($params['content'])){
                $this->ajaxReturn(4001, '请添加内容');      //判断数据是否输入    
            }
            if(!is_numeric($params['read_count'])){
                $this->ajaxReturn(4001, '阅读数是数字');      
            }
            if(empty($id)){
                $case=array();
                $case['Case']['title'] = $params['title'];
                $case['Case']['class'] = $params['class'];
                $case['Case']['min_content'] = $params['min_content'];
                $case['Case']['content'] = $params['content'];
                $case['Case']['web_title'] = $params['web_title'];
                $case['Case']['web_keyword'] = $params['web_keyword'];
                $case['Case']['web_descrion'] = $params['web_descrion'];
                $case['Case']['case_img'] = $params['case_img'];
                $case['Case']['case_index'] = isset($params['case_index'])?1:2;
                $case['Case']['create_time'] = date("Y-m-d",time());
                $case['Case']['update_time'] = date("Y-m-d",time());
                $case['Case']['author'] = $params['author'];
                $case['Case']['read_count'] = $params['read_count'];
                if(!$this->Case->save($case)){                          //存储数据，如果失败则报错
                    $this->ajaxReturn(4004, '操作失败，请稍后再试'); 
                }else{
                    $this->ajaxReturn(200, '操作成功！','','/admin/case_manage');
                }
            }else{
                $case=array();
                $case['Case']['id'] = $id;                               //写入数值
                $case['Case']['title'] = $params['title'];
                $case['Case']['class'] = $params['class'];
                $case['Case']['min_content'] = $params['min_content'];
                $case['Case']['content'] = $params['content'];
                $case['Case']['web_title'] = $params['web_title'];
                $case['Case']['web_keyword'] = $params['web_keyword'];
                $case['Case']['web_descrion'] = $params['web_descrion'];
                $case['Case']['case_img'] = $params['case_img'];
                $case['Case']['case_index'] = isset($params['case_index'])?1:2;
                $case['Case']['update_time'] = date("Y-m-d",time());
                $case['Case']['author'] = $params['author'];
                $case['Case']['read_count'] = $params['read_count'];
                if(!$this->News->save($case)){                          //存储数据，如果失败则报错
                    $this->ajaxReturn(4004, '操作失败，请稍后再试'); 
                }else{
                    $this->ajaxReturn(200, '操作成功！','','/admin/case_manage');
                }
            }
        }
        $tdk = $this->Tdk->find('first',array('conditions' => array('cid'=>0)));
        $caseclass = $this->CaseClass->find('all',array());
        if(!empty($id)){
            $case=$this->News->findById($id);
            $read_count=$case['Case']['read_count'];
            $nclass=[];
            $nclass['id']=$case['Case']['class'];
            $tclass=$this->CaseClass->find('first',array('conditions' => array('id'=>$nclass['id'])));
            if(!empty($tclass)){
                $nclass['name']=$tclass['CaseClass']['class_name'];
            }else{
                $nclass['name']="";
                $nclass['id']=1000;
            }
            $tdk['Tdk']['web_title']=$case['Case']['web_title'];
            $tdk['Tdk']['web_keyword']=$case['Case']['web_keyword'];
            $tdk['Tdk']['web_descrion']=$case['Case']['web_descrion'];
            $this->set('case',$case);
            if($case['Case']['case_index']==1){
                $checkbox=1;
            }else{
                $checkbox=0;
            }
        }else{
            $read_count=100;
            $nclass=[];
            $nclass['id']=0;
            $nclass['name']="选择分类";
            $checkbox=0;
        }
        $this->set('nclass',$nclass);
        $this->set('checkbox',$checkbox);
        $this->set('read_count',$read_count);
        $this->set('tdk',$tdk);
        $this->set('class',$caseclass);
        $nav="f2";
        $this->set('nav',$nav);
        $mav6="bb";
        $this->set('mav6',$mav6); 
    }

    //案例修改
    function case_update(){
        $this->case_add();                                              //加载case_add的方法
        $this->render('/admin/case_add');                               //跳回添加页面
        $nav="f2";
        $this->set('nav',$nav);
        $mav6="bb";
        $this->set('mav6',$mav6); 
    }

    //案例删除
    function case_del(){                                         
        $params = $this->request->query;                                //获取数据
        $this->data_delete('Case',array('id'=>$params['id']));        //调用父类方法data_delete()
        $this->render('/admin/case_manage');                             //跳回管理页面
    }

    //案例管理

    function case_manage(){   
        $id = $this->request->query('id');
        $action = $this->request->query('action');
        $this->loadModel('Case');                                  //加载数据表news
        $this->loadModel('CaseClass');                               //加载数据表newsclass
        $this->paginate = array(                                        //利用分页取出数据
            'conditions' =>array('class !=' => 1000 ),
            'order'=>array('id' =>'desc'),
            'limit'=>10
        );
        try{
            $case = $this->paginate('Case');
        }catch(NotFoundException $e){
            $case = array();
        }
        foreach ($case as $key => $value) { 
        if(intval($value['Case']['class'])>0){ 
            $info = $this->CaseClass->findById(intval($value['Case']['class']));
            if(isset($info['CaseClass']['id'])){   
                $case[$key]['Case']['class_name']=$info['CaseClass']['class_name'];
                }else{
                    $case[$key]['Case']['class_name'] = "分类已删";
                }
            }else{
                $case[$key]['Case']['class_name'] ="分类已删";
            }
        } 
        if(!empty($action) && !empty($id)){
            if(!$this->Case->delete(array('id'=>$id))){                        //存储数据，如果失败则报错
            $this->ajaxReturn(4004, '操作失败，请稍后再试'); 
            }else{
                $this->ajaxReturn(300, '操作成功！');
                $this->render('/admin/case_manage');                               //跳回添加页面 
            }
        }
        $this->set('case',$case); 
        $nav="f2";
        $this->set('nav',$nav);      
        $mav6="bb";
        $this->set('mav6',$mav6);                       
    }

    //留言管理

    function guestbook(){   
        $this->loadModel('Guestbook');                                  //加载数据表guestbook
        $id = $this->request->query('id');
        $action = $this->request->query('action');
        $guestbook = $this->Guestbook->find('all',array());
        $read=[];
        $display=[];
        if(!empty($action) && !empty($id)){
            if(!$this->Guestbook->delete(array('id'=>$id))){                        //存储数据，如果失败则报错
                $this->ajaxReturn(4004, '操作失败，请稍后再试'); 
            }else{
                $this->ajaxReturn(300, '操作成功！');
                $this->render('/admin/guestbook');                               //跳回添加页面 
            } 
        }
        if(!empty($guestbook['guestbook']['read'])){
            $read=$guestbook['guestbook']['read'];
        }
        if($read=='1'){
            $read='是';
        }else{
            $read='否';
        }
        if(!empty($guestbook['guestbook']['display'])){
            $display=$guestbook['guestbook']['display'];
        }
        if($display=='1'){
            $display='是';
        }else{
            $display='否';
        }
        $this->set('read',$read);
        $this->set('display',$display);
        $this->set('guestbook',$guestbook);
        $nav="h";
        $this->set('nav',$nav);
    }

    //留言详情

    function guestbook_info(){
        $params = $this->request->data;                             //获取post数据
        $this->loadModel('Guestbook');                                  //加载数据表guestbook
        $id = $this->request->query('id');
        if(!empty($id)){
            $guestbook = $this->Guestbook->find('first',array());
            $this->set('guestbook',$guestbook);
        }
        if($this->request->is('post')){                         //如果成功获取请求参数
            $guestbook = array();
            $guestbook['Guestbook']['id'] = $id;
            $guestbook['Guestbook']['replay'] = $params['replay'];                               //写入数值
            $guestbook['Guestbook']['display'] = isset($params['display'])?1:2;
            $guestbook['Guestbook']['read'] =1;
            $guestbook['Guestbook']['update_time'] = date("Y-m-d",time());
            if(!$this->Guestbook->save($guestbook)){                          //存储数据，如果失败则报错
                $this->ajaxReturn(4004, '操作失败，请稍后再试'); 
            }else{
                $this->ajaxReturn(200, '操作成功！','','/admin/guestbook');
            }
        }
        $nav="h";
        $this->set('nav',$nav);
    }

    //新增岗位

    function hr_add(){ 
        $params = $this->request->data;                             //获取post数据  
        $this->loadModel('Hrdemand');                                  //加载数据表guestbook
        $id = $this->request->query('id');
        if($this->request->is('post')){                         //如果成功获取请求参数
            if(empty($params['title'])){
                $this->ajaxReturn(4001, '请填写岗位名称');      //判断数据是否输入    
            }
            $hr = array();
            $hr['Hrdemand']['title'] = $params['title'];
            $hr['Hrdemand']['phone'] = $params['phone'];
            $hr['Hrdemand']['content'] = $params['content'];
            $hr['Hrdemand']['money'] = $params['money'];
            $hr['Hrdemand']['address'] = $params['address'];
            $hr['Hrdemand']['number'] = $params['number'];
            $hr['Hrdemand']['create_time'] = date("Y-m-d",time());
            if(!empty($id)){
                $hr['Hrdemand']['id'] = $id;
                if(!$this->Hrdemand->save($hr)){                          //存储数据，如果失败则报错
                    $this->ajaxReturn(4004, '操作失败，请稍后再试'); 
                }else{
                    $this->ajaxReturn(200, '操作成功！','','/admin/hr_manage');
                }
            }else{
                if(!$this->Hrdemand->save($hr)){                          //存储数据，如果失败则报错
                    $this->ajaxReturn(4004, '操作失败，请稍后再试'); 
                }else{
                    $this->ajaxReturn(200, '操作成功！','','/admin/hr_manage');
                }
            }
        }
        if(!empty($id)){
            $hr = $this->Hrdemand->find('first',array('conditions'=>array('id' => $id)));
            $this->set('hr',$hr);
        }
        $nav="i";
        $this->set('nav',$nav);
    }

    //岗位管理

    function hr_manage(){   
        $this->loadModel('Hrdemand');                                  //加载数据表hurdemand
        $id = $this->request->query('id');
        $action = $this->request->query('action');
        $hr = $this->Hrdemand->find('all',array());
        if(!empty($action) && !empty($id)){
            if(!$this->Hrdemand->delete(array('id'=>$id))){                        //存储数据，如果失败则报错
                $this->ajaxReturn(4004, '操作失败，请稍后再试'); 
            }else{
                $this->ajaxReturn(300, '操作成功！');
                $this->render('/admin/hr_manage');                               //跳回添加页面 
            } 
        }
        $this->set('hr',$hr);
        $nav="i";
        $this->set('nav',$nav);
    }

    //会员管理

    function user_manage(){   
        $this->loadModel('User');                                  //加载数据表hurdemand
        $id = $this->request->query('id');
        $action = $this->request->query('action');
        $user = $this->User->find('all',array());
        if(!empty($action) && !empty($id)){
            if(!$this->User->delete(array('id'=>$id))){                        //存储数据，如果失败则报错
                $this->ajaxReturn(4004, '操作失败，请稍后再试'); 
            }else{
                $this->ajaxReturn(300, '操作成功！');
                $this->render('/admin/user_manage');                               //跳回添加页面 
            } 
        }
        $this->set('user',$user);
        $nav="j";
        $this->set('nav',$nav);
    }
}